package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.AdminCommentListVo;
import com.cskaoyan.service.CommentService;
import com.cskaoyan.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 商品管理中的评论模块
 * @author: Tao
 * @time: 2022/12/28 22:16
 */
@RestController
@RequestMapping("admin/comment")
public class CommentController {

    @Autowired
    CommentService commentService;

    /**
     *  有bug！有bug！有bug！ 没有进行图片url转化，不能正常显示，应该将字符串转化为数组。
     * @param info
     * @param userId
     * @param valueId
     * @return 满足特点条件的评价
     */
    @RequestMapping("list")
    public AdminCommentListVo list(BasePageInfo info, String userId, String valueId) {
        //过滤掉参数值不对的情况（不是int类型）
        //id是用户的id
        Integer id = null;
        if (!StringUtil.isEmpty(userId)) {
            try {
                id = Integer.valueOf(userId);
            } catch (Exception e) {
                return AdminCommentListVo.err("参数值不对");
            }
        }
        //goodId是商品的id
        Integer goodsId = null;
        if (!StringUtil.isEmpty(valueId)) {
            try {
                goodsId = Integer.valueOf(valueId);
            } catch (Exception e) {
                return AdminCommentListVo.err("参数值不对");
            }
        }
        //参数值都对才会到这里，此时id仍然可能为null
        CommonData<MarketComment> data = commentService.getList(info, id, goodsId);
        return AdminCommentListVo.ok(data);
    }

    /**
     * 删除选中的评论
     * @param marketComment
     * @return Vo：{"errno":0,"errmsg":"成功"}
     */
    @RequestMapping("delete")
    public AdminCommentListVo delete(@RequestBody MarketComment marketComment) {
        // 调用service 进行删除操作
        commentService.delete(marketComment);
        // 响应
        // {"errno":0,"errmsg":"成功"}
        return AdminCommentListVo.ok("成功");
    }

}
